﻿//基础设置 - 用户管理
layui.use(['form', 'jquery', 'laydate', 'dialog', 'table'
    , 'laypage', 'ajaxUtil', 'permissionUtil', 'switchTab', 'tree'], function () {
        var table = layui.table,
            laypage = layui.laypage,
            $ = layui.jquery,
            dialog = layui.dialog,
            ajaxutil = layui.ajaxUtil,
            laydate = layui.laydate,
            form = layui.form,
            permissionUtil = layui.permissionUtil,
            switchTab = layui.switchTab
            , tree = layui.tree;
        permissionUtil.setting(160010);

        //表格渲染
        table.render({
            id: 'cn_GridBasicSettingRoleInfo'
            , elem: '#grid_BasicSettingRoleInfo'
            , method: 'post'
            , url: '/basicSetting/roleinfo/list'
            //启动分页
            , page: true
            , height: 'full-100'
            , loading: true
            , text: { none: '未查询到数据' }
            , limit: 20
            , limits: [20, 50, 100]
            //禁用前端自动排序
            , autoSort: false
            , where: {
                roleName: ''
            }
            , request: {
                pageName: 'pageIndex'
                , limitName: 'pageSize'
            }
            , response: {
                //statusName: 'status', //规定数据状态的字段名称，默认：code
                statusCode: 200, //规定成功的状态码，默认：0
                //msgName: 'hint', //规定状态信息的字段名称，默认：msg
                countName: 'total', //规定数据总数的字段名称，默认：count
                //dataName: 'rows' //规定数据列表的字段名称，默认：data
            }
            , cols: [[
                { field: 'roleId', width: 80, title: '编号' }
                , { field: 'roleName', width: 150, title: '角色名称' }
                , { field: 'createTime', width: 150, title: '创建时间' }
                , { fixed: 'right', title: '操作', toolbar: '#barOperation', width: 120 }
            ]]
        });

        //监听排序事件 
        table.on('sort(ft_BasicSettingRoleInfo)', function (obj) {
            if (obj.type !== null) {
                table.reload('cn_GridBasicSettingRoleInfo', {
                    initSort: obj
                    , where: {
                        sortName: obj.field //排序字段
                        , sortOrder: obj.type //排序方式
                        , roleName: $('#txt_BasicSettiingRoleName').val()
                    }
                });
            }
        });

        //监听行工具事件
        table.on('tool(ft_BasicSettingRoleInfo)', function (obj) {
            var rowData = obj.data;
            if (obj.event === 'del') {
                var roleId = rowData.roleId;
                dialog.alert.confirm({
                    message: '确定删除该角色吗？'
                    , success: function (index) {
                        dialog.close(index);
                        ajaxutil.request({
                            url: '/basicSetting/roleinfo/del',
                            data: {
                                roleId: roleId
                            },
                            success: function (res) {
                                if (res.code === 200) {
                                    dialog.alert.msg('删除成功');

                                    var pageIndex = $(".layui-laypage-skip").find("input").val();
                                    //刷新页面
                                    loadData({
                                        roleName: $('#txt_BasicSettiingRoleName').val()
                                    }, pageIndex)
                                } else {
                                    dialog.alert.fail(res.msg);
                                }
                            }
                        });
                    }
                });
            }
            else if (obj.event === 'edit') {
                initEditForm(rowData);

                //获取角色权限id
                getRoleRight(rowData.roleId);

                dialog.open({
                    dialogId: 'dialog_BasicSettingRoleInfo'
                    , width: 450
                    , height: 500
                    , title: '修改角色'
                    , ok: function (index) {
                        editRole(index, rowData.roleId);
                    }
                    , cancel: function () {

                    }
                });
            }
        });

        var loadData = function (param, pageIndex) {
            //执行重载
            table.reload('cn_GridBasicSettingRoleInfo', {
                page: {
                    curr: pageIndex //刷新指定页
                }
                , where: param
            });
        }

        //筛选
        $('#btn_BasicSettingRoleInfoSearch').on('click', function () {
            loadData({
                roleName: $('#txt_BasicSettiingRoleName').val()
            }, 1)
        });

        //添加
        $('#btn_BasicSettingRoleInfo_Add').on('click', function () {
            clearForm();
            reloadTree();

            dialog.open({
                dialogId: 'dialog_BasicSettingRoleInfo'
                , width: 450
                , height: 500
                , title: '添加角色'
                , ok: function (index) {
                    addRole(index);
                }
                , cancel: function () {

                }
            });
        });

        var addRole = function (index) {
            var roleName = $(".cs_BasicSettingRoleName").val();

            getCheckedRoleId();

            ajaxutil.request({
                url: '/basicSetting/roleInfo/add',
                data: {
                    roleName: roleName,
                    rightIds: rightIds
                },
                success: function (res) {
                    if (res.code === 200) {
                        dialog.close(index);
                        dialog.alert.success('添加成功');

                        $('#txt_BasicSettiingRoleName').val('');
                        //刷新页面
                        loadData({
                            roleName: ''
                        }, 1)

                    } else {
                        dialog.alert.fail(res.msg);
                    }
                }
            });
        }

        var editRole = function (index, roleId) {
            var roleName = $(".cs_BasicSettingRoleName").val();

            getCheckedRoleId();

            ajaxutil.request({
                url: '/basicSetting/roleInfo/edit',
                data: {
                    roleId: roleId
                    , roleName: roleName
                    , rightIds: rightIds
                },
                success: function (res) {
                    if (res.code === 200) {
                        dialog.close(index);
                        dialog.alert.success('修改成功');

                        //当前页码（如果当前页存在多个表格，则需要换种写法）
                        var pageIndex = $(".layui-laypage-skip").find("input").val();

                        //刷新指定页面
                        loadData({
                            roleName: $('#txt_BasicSettiingRoleName').val()
                        }, pageIndex);
                    } else {
                        dialog.alert.fail(res.msg);
                    }
                }
            });
        }

        var getRoleRight = function (roleId) {
            reloadTree();

            ajaxutil.request({
                url: '/basicSetting/roleInfo/getright',
                data: {
                    roleId: roleId
                },
                success: function (res) {
                    if (res.code === 200) {
                        console.log(res);
                        var _roleRightIds = [];
                        res.data.forEach(function (item, index) {
                            _roleRightIds.push(item.rightId);
                        });
                        //设置权限树
                        tree.setChecked('roleTreeId', _roleRightIds); //勾选指定节点
                    }
                }
            });
        }

        var initEditForm = function (rowData) {
            $(".cs_BasicSettingRoleName").val(rowData.roleName);

            //更新 lay-filter="ft_BasicSettingForm" 所在容器内的全部表单状态
            form.render(null, 'ft_BasicSettingFormRoleInfo');
        }

        ///清空表单
        var clearForm = function () {
            $(".cs_BasicSettingRoleName").val('');

            //更新 lay-filter="ft_BasicSettingForm" 所在容器内的全部表单状态
            form.render(null, 'ft_BasicSettingFormRoleInfo');
        }

        var getCheckedRoleId = function () {
            var checkedData = tree.getChecked('roleTreeId'); //获取选中节点的数据
            rightIds = [];
            getRightIds(checkedData);
        }

        var rightIds = [];
        //递归获取选中的角色id
        var getRightIds = function (checkedData) {
            checkedData.forEach(function (item, index) {
                debugger;
                rightIds.push(item.id);
                if (item.children != null && item.children.length > 0) {
                    getRightIds(item.children);
                }
            });
        }

        //清除选中项
        var clearChecked = function () {
            //var checkedData = tree.getChecked('roleTreeId'); //获取选中节点的数据
            //rightIds = [];
            //getRightIds(checkedData);
            //dialog_BasicSettingRoleInfo
        }

        var data = [{
            title: '表格管理'
            , id: 1000
            , field: ''
            //, checked: true
            , spread: false
            , children: [{
                title: '基本表格'
                , id: 100000
                , field: ''
                , children: [{
                    title: '查询'
                    , id: 10000000
                    , field: ''
                }, {
                    title: '添加'
                    , id: 10000010
                    , field: ''
                }, {
                    title: '编辑'
                    , id: 10000020
                    , field: ''
                }, {
                    title: '删除'
                    , id: 10000030
                    , field: ''
                }]
            }, {
                title: '复选框表格1'
                , id: 100010
                , children: [{
                    title: '查询'
                    , id: 10001000
                    , field: ''
                }, {
                    title: '获取选中编号'
                    , id: 10001010
                    , field: ''
                }]
            }, {
                title: '复选框表格2'
                , id: 100015
                , field: ''
                , children: [{
                    title: '查询'
                    , id: 10001500
                    , field: ''
                }, {
                    title: '获取多列选中内容'
                    , id: 10001510
                    , field: ''
                }]
            }, {
                title: '设置单元格样式'
                , id: 100020
                , field: ''
                , children: [{
                    title: '查询'
                    , id: 10002000
                    , field: ''
                }]
            }]
        }, {
            title: '表单'
            , id: 1100
            , field: ''
            , spread: false
            , children: [{
                title: '下拉框'
                , id: 110000
                , field: ''
                , children: [{
                    title: '获取值'
                    , id: 11000000
                    , field: ''
                }, {
                    title: '设置值'
                    , id: 11000010
                    , field: ''
                }]
            }, {
                title: 'CheckBox复选框'
                , id: 110010
                , field: ''
                , children: [{
                    title: '获取选中值'
                    , id: 11001000
                    , field: ''
                }]
            }]
        }, {
            title: '图表'
            , id: 1200
            , field: ''
            , children: [{
                title: '折线图'
                , id: 120000
                , field: ''
                , children: [{
                    title: '查询'
                    , id: 12000000
                    , field: ''
                }]
            }, {
                title: '柱状图'
                , id: 120010
                , field: ''
                , children: [{
                    title: '查询'
                    , id: 12001000
                    , field: ''
                }]
            }, {
                title: '饼图'
                , id: 120020
                , field: ''
                , children: [{
                    title: '查询'
                    , id: 12002000
                    , field: ''
                }]
            }]
        }, {
            title: 'Excel操作'
            , id: 1300
            , field: ''
            , children: [{
                title: '导入Excel'
                , id: 130000
                , field: ''
                , children: [{
                    title: '查询'
                    , id: 13000000
                    , field: ''
                }, {
                    title: '导入Excel数据'
                    , id: 13000010
                    , field: ''
                }, {
                    title: '下载模板'
                    , id: 13000020
                    , field: ''
                }]
            }, {
                title: '导出Excel'
                , id: 130010
                , field: ''
                , children: [{
                    title: '查询'
                    , id: 13001000
                    , field: ''
                }, {
                    title: '导出Excel'
                    , id: 13001010
                    , field: ''
                }]
            }]
        },
        {
            title: '文件管理'
            , id: 1400
            , field: ''
            , children: [{
                title: '文件上传'
                , id: 140000
                , field: ''
                , children: [{
                    title: '上传文件'
                    , id: 14000000
                    , field: ''
                }]
            }, {
                title: '在线预览'
                , id: 140010
                , field: ''
                , children: [{
                    title: '预览'
                    , id: 14001000
                    , field: ''
                }]
            }, {
                title: '图片裁剪上传'
                , id: 140020
                , field: ''
                , children: [{
                    title: '上传图片'
                    , id: 14002000
                    , field: ''
                }]
            }]
        }, {
            title: '通讯管理'
            , id: 1500
            , field: ''
            , children: [{
                title: '发送邮件'
                , id: 150000
                , field: ''
                , children: [{
                    title: '发送'
                    , id: 15000000
                    , field: ''
                }]
            }]
        }, {
            title: '基础设置'
            , id: 1600
            , field: ''
            , children: [{
                title: '用户管理'
                , id: 160000
                , field: ''
                , children: [{
                    title: '查询'
                    , id: 16000000
                    , field: ''
                }, {
                    title: '添加'
                    , id: 16000010
                    , field: ''
                }, {
                    title: '启用/禁用'
                    , id: 16000020
                    , field: ''
                }, {
                    title: '分配角色'
                    , id: 16000030
                    , field: ''
                }, {
                    title: '编辑'
                    , id: 16000040
                    , field: ''
                }, {
                    title: '重置密码'
                    , id: 16000050
                    , field: ''
                }, {
                    title: '删除'
                    , id: 16000060
                    , field: ''
                }]
            }, {
                title: '角色管理'
                , id: 160010
                , field: ''
                , children: [{
                    title: '查询'
                    , id: 16001000
                    , field: ''
                }, {
                    title: '添加'
                    , id: 16001010
                    , field: ''
                }, {
                    title: '编辑'
                    , id: 16001020
                    , field: ''
                }, {
                    title: '删除'
                    , id: 16001030
                    , field: ''
                }]
            }]
        }];

        var loadRoleTree = function () {
            tree.render({
                elem: '#cs_BasicSettingRoleTree'
                , data: data
                , showCheckbox: true  //是否显示复选框
                , id: 'roleTreeId'
                //, isJump: true //是否允许点击节点时弹出新窗口跳转
                //, click: function (obj) {
                //    var data = obj.data;  //获取当前点击的节点数据
                //    //layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                //}
            });
        }

        var reloadTree = function () {
            tree.reload('roleTreeId', {

            });
        }

        loadRoleTree();
    });